#ifndef _ODN_ELEMENT_H
#define _ODN_ELEMENT_H

//------Standard Library Headers------
#include <iostream>
//------------------------------------

using std::cout;
using std::endl;

namespace ouden
{
	
	//Base class for most of engine
	class ODN_Element
	{
		
	public:
		
		//Constructor
		ODN_Element() : refcount(1)		{}		//Initialise the reference counter to 1
		
		//Destructor
		virtual ~ODN_Element()			{}
	
		//Called by anything that needs to use the object
		void Grab()	
		{
		
			//Increase the reference counter
			refcount++; 
		
		}
		
		//Called by anything no longer needing the object
		bool Release()
		{
			
			#if defined(_DEBUG)
				if (refcount <= 0) {
					cout << "Error - Reference count less than 0, someone not using Grab() and Release() correctly" << endl;
				}
			#endif
			
			//Decrease the reference count
			refcount--;
			
			//If the reference count is 0 then delete the object
			if (!refcount) {
				
				delete this;
				return true;
				
			}
			
			return false;
			
		}
	
	private:
		
		//Reference counter
		unsigned refcount;
		
	};

} //namespace ouden

#endif //_ODN_ELEMENT_H
